//
//  CustomPhotosCell.m
//  高仿暴走斗图
//
//  Created by key on 16/6/3.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "CustomPhotosCell.h"

#define SELECT_IMG_HIGHT    20.0f

@interface CustomPhotosCell()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *selectImageView;
@property (nonatomic, assign, readonly) BOOL isZoomIn;

@end

@implementation CustomPhotosCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.imageView = [NSObject createImgWithImage:nil
                                               hImage:nil
                                        clipsToBounds:YES
                                              vcModel:UIViewContentModeScaleAspectFill];
        self.imageView.frame = self.contentView.bounds;
        [self.contentView addSubview:self.imageView];
        
        self.selectImageView = [NSObject createImgWithImage:CREATE_IMG(SELECT_IMG)
                                                     hImage:CREATE_IMG(SELECT_IMG)
                                              clipsToBounds:YES
                                                    vcModel:UIViewContentModeScaleToFill];
        self.selectImageView.frame = CGRectMake(self.imageView.viewMaxY - SELECT_IMG_HIGHT, 0, SELECT_IMG_HIGHT, SELECT_IMG_HIGHT);
        [self.selectImageView.layer setCornerRadius:self.selectImageView.viewHeight];
        [self.contentView addSubview:self.selectImageView];
        self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.0f, 0.0f);
        
        _isZoomIn = NO;
    }
    return self;
}

- (void)setPhotoImage:(UIImage *)image {
    
    if (!self.imageView || !image) {
        return;
    }
    _object = image;
    self.imageView.image = image;
    self.imageView.highlightedImage = image;
}

- (void)selected:(BOOL)isSelected {
    
    if (isSelected) {
        
        self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0f, 1.0f);
    } else {
        
        self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.0f, 0.0f);
    }
}


- (BOOL)transformsAnimation {
    
    @weakify(self);
    if (!self.isZoomIn) {
        @strongify(self);
        [UIView animateWithDuration:[self duration] animations:^{
            self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2f, 1.2f);
        } completion:^(BOOL finished) {
           [UIView animateWithDuration:[self duration]/2 animations:^{
               self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0f, 1.0f);
           } completion:^(BOOL finished) {
               
           }];
        }];
    } else {
        
        [UIView animateWithDuration:[self duration]/2 animations:^{
            self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2f, 1.2f);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:[self duration] animations:^{
                self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.2f, 0.2f);
            } completion:^(BOOL finished) {
                self.selectImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.0f, 0.0f);
            }];
        }];
    }
    _isZoomIn = !self.isZoomIn;
    return _isZoomIn;
}


- (CGFloat)duration {
    
    return 0.4f;
}
@end
